package com.tianqu.tms.core.util;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.log4j.chainsaw.Main;

import com.tianqu.tms.core.base.Logger;

public final class DateUtil {
	private static Logger logger = Logger.getLogger(DateUtil.class);

	private DateUtil() {
		super();
	}

	/**
	 * Parse yyyyMMdd String to java.util.Date
	 * 
	 * @param date8
	 *            yyyyMMdd String
	 * @return Date
	 * @throws ParseException
	 */
	public static Date string8ToDate(String date8) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		return (Date) sdf.parse(date8);
	}

	public static Date string102ToDate(String date101) throws ParseException {
		DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = sdf.parse(date101);
		return date;
	}

	public static Date stringYYYYMMToDate(String date) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
		return sdf.parse(date);
	}

	public static Date stringYYYYToDate(String date) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
		return sdf.parse(date);
	}

	/**
	 * Parse yyMMdd String to java.util.Date
	 * 
	 * @param date6
	 *            yyMMdd String
	 * @return Date
	 * @throws ParseException
	 */
	public static Date string6ToDate(String date6) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyMMdd");
		return sdf.parse(date6);
	}

	/**
	 * Parse yyyy/MM/dd String to java.util.Date
	 * 
	 * @param date10
	 *            yyyy/MM/dd String
	 * @return Date
	 * @throws ParseException
	 */
	public static Date string10ToDate(String date10) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
		return sdf.parse(date10);
	}

	public static Date string101ToDate(String date101) throws ParseException {
		DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = sdf.parse(date101);
		return date;
	}

	/**
	 * Parse yyyy/MM/dd HH:mm String to java.util.Date
	 * 
	 * @param date12
	 *            yyyy/MM/dd HH:mm String
	 * @return Date
	 * @throws ParseException
	 */
	public static Date string12ToDate(String date12) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm");
		return sdf.parse(date12);
	}

	/**
	 * Parse java.util.Date to yyMMdd String
	 * 
	 * @param date
	 *            java.util.Date
	 * @return String
	 */
	public static String DateToString6(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyMMdd");
		return sdf.format(date);
	}

	/**
	 * Parse java.util.Date to yyyyMMdd String
	 * 
	 * @param date
	 *            java.util.Date
	 * @return String
	 */
	public static String DateToString8(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		return sdf.format(date);
	}

	/**
	 * Parse java.util.Date to yyyy/MM/dd String
	 * 
	 * @param date
	 *            java.util.Date
	 * @return String
	 */
	public static String DateToString10(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
		return sdf.format(date);
	}

	/**
	 * Parse java.util.Date to yyyy/MM/dd String
	 * 
	 * @param date
	 *            java.util.Date
	 * @return String
	 */
	public static String DateToString11(Date date) {
		if (date == null)
			return "";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.format(date);
	}

	/**
	 * Parse java.util.Date to yyyy/MM/dd String
	 * 
	 * @param date
	 *            java.util.Date
	 * @return String
	 */
	public static String DateToString17(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		return sdf.format(date);
	}

	/**
	 * Parse java.util.Date to yyyy/MM/dd String
	 * 
	 * @param date
	 *            java.util.Date
	 * @return String
	 */
	public static String DateToString12(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sdf.format(date);
	}

	/**
	 * Parse java.util.Date to yyyy/MM/dd String
	 * 
	 * @param date
	 *            java.util.Date
	 * @return String
	 */
	public static String DateToString13(Timestamp date) {
		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
		return sdf.format(date);
	}

	public static String DateToString14(Timestamp date) {
		SimpleDateFormat sdf = new SimpleDateFormat("HH");
		return sdf.format(date);
	}

	public static String DateToString15(Timestamp date) {
		SimpleDateFormat sdf = new SimpleDateFormat("mm");
		return sdf.format(date);
	}

	/**
	 * Parse java.util.Date to HH:mm String
	 * 
	 * @param date
	 *            java.util.Date
	 * @return String
	 */
	public static String DateToString20(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
		return sdf.format(date);
	}

	/**
	 * Parse java.util.Date to HH:mm String
	 * 
	 * @param date
	 *            java.util.Date
	 * @return String
	 */
	public static String DateToString21(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
		return sdf.format(date);
	}
	public static boolean compareDate102(Date date1, Date date2) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String str1 = sdf.format(date1);
		String str2 = sdf.format(date2);
		if (str1.equals(str2)) {
			return true;
		}
		return false;
	}
	public static boolean comparDate(Date date1, Date date2) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String str1 = sdf.format(date1);
		String str2 = sdf.format(date2);
		if (str1.equals(str2)) {
			return true;
		}
		return false;
	}


	/**
	 * 时间比较
	 * 
	 */
	public static boolean compareDateTime(Date date1, Date date2) {
		boolean flag = false;
		SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMddhhmmss");
		String str1 = sdf1.format(date1);
		String str2 = sdf1.format(date2);
		if (Integer.parseInt(str1) <= Integer.parseInt(str2)) {
			flag = true;
		}
		return flag;
	}

	public static boolean compareStringTime(String date1, String date2) {
		boolean flag = false;
		int res = date1.compareTo(date2);
		if (res < 0) {
			// System.out.println("c1小于c2");
			flag = false;
		} else {
			// System.out.println("c1大于c2");
			flag = true;
		}

		return flag;
	}

	/**
	 * 获取当前系统时间 返回String类型
	 * 
	 * @param args
	 */
	public static String getCurrentTime() {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式
		return df.format(new Date());
	}

	public static String getCurrentTime1() {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");// 设置日期格式
		return df.format(new Date());
	}

	/**
	 * 获取当前系统时间 返回String类型
	 * 
	 * @param args
	 */
	public static String getCurrentTimeStr() {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");// 设置日期格式
		return df.format(new Date());
	}

	/**
	 * 获取当前系统时间 返回String类型
	 * 
	 * @param args
	 */
	public static String getCurrentTimeStr4() {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM");// 设置日期格式
		return df.format(new Date());
	}

	/**
	 * 获取当前系统时间 返回String类型
	 * 
	 * @param args
	 */
	public static String getCurrentTimeStr2() {
		SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd");// 设置日期格式
		return df.format(new Date());
	}

	/**
	 * 获取当前系统时间 返回String类型
	 * 
	 * @param args
	 */
	public static String getCurrentTimeStr3() {
		SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm");// 设置日期格式
		return df.format(new Date());
	}

	/**
	 * 获取当前系统时间 返回String类型
	 * 
	 * @param args
	 */
	public static String getCurrentTimeStr1() {
		SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");// 设置日期格式
		return df.format(new Date());
	}

	/**
	 * StringToDate
	 * 
	 * @param date
	 * @throws ParseException
	 */
	public static Date getStringToDate(String date) throws ParseException {
		SimpleDateFormat sim = new SimpleDateFormat("HH:mm");
		Date d = sim.parse(date);
		return d;
	}

	/**
	 * StringToDate
	 * 
	 * @param date
	 * @throws ParseException
	 */
	public static Date getStringToDateZ(String date) throws ParseException {
		SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date d = sim.parse(date);
		return d;
	}

	/**
	 * 获得两个时间段想差多少秒
	 * 
	 * @param startTime
	 * @param endtime
	 * @return
	 * @throws ParseException
	 */
	public static long getTimePoor(String nowDate, String endtime)
			throws ParseException {

		String format = "yyyy-mm-dd HH:mm:ss";
		SimpleDateFormat sf = new SimpleDateFormat(format);
		Date eDate = sf.parse(DateToString12(new Date()));
		Date sDate = sf.parse(nowDate + " " + endtime + ":00");
		return sDate.getTime() - eDate.getTime();

	}

	/**
	 * TimeStamp-->String
	 * 
	 * @param timeS
	 * @return
	 */
	public static String getTimeByTimeStamp(Timestamp timeS) {
		if (timeS != null) {
			SimpleDateFormat fm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			return fm.format(timeS);

		} else {
			return null;
		}
	}

	/**
	 * TimeStamp-->String
	 * 
	 * @param timeS
	 * @return
	 */
	public static String getTimeByTimeStamp1(Timestamp timeS) {
		if (timeS != null) {
			SimpleDateFormat fm = new SimpleDateFormat("yyyy-MM-dd HH:mm");
			return fm.format(timeS);

		} else {
			return null;
		}
	}

	/**
	 * TimeStamp-->String
	 * 
	 * @param timeS
	 * @return
	 */
	public static String getDateByTimeStamp(Timestamp timeS) {
		if (timeS != null) {
			SimpleDateFormat fm = new SimpleDateFormat("yyyy-MM-dd");
			return fm.format(timeS);

		} else {
			return null;
		}
	}

	/**
	 * TimeStamp-->String
	 * 
	 * @param timeS
	 * @return
	 */
	public static String getDateByTimeStampHM(Timestamp timeS) {
		if (timeS != null) {
			SimpleDateFormat fm = new SimpleDateFormat("yyyy-MM-dd HH:mm");
			return fm.format(timeS);

		} else {
			return null;
		}
	}

	/**
	 * TimeStamp-->String
	 * 
	 * @param timeS
	 * @return
	 */
	public static String getHHByTimeStamp(Timestamp timeS) {
		if (timeS != null) {
			SimpleDateFormat fm = new SimpleDateFormat("HH:mm");
			return fm.format(timeS);

		} else {
			return null;
		}
	}

	/**
	 * TimeStamp-->String
	 * 
	 * @param timeS
	 * @return
	 */
	public static Timestamp getStringToTimeStamp(String timeS) {
		return Timestamp.valueOf(DateToString11(new Date()) + " " + timeS
				+ ":00");
	}

	public static boolean isValidDate(String str) {
		DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		try {
			Date date = (Date) formatter.parse(str);
			return str.equals(formatter.format(date));
		} catch (Exception e) {
			return false;
		}
	}

	/**
	 * 獲得當前時間的下一天
	 * 
	 * @param specifiedDay
	 * @return
	 */
	public static String getSpecifiedDayAfter(String specifiedDay) {
		Calendar c = Calendar.getInstance();
		Date date = null;
		try {
			date = new SimpleDateFormat("yyyy-MM-dd").parse(specifiedDay);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		c.setTime(date);
		int day = c.get(Calendar.DATE);
		c.set(Calendar.DATE, day + 1);

		String dayAfter = new SimpleDateFormat("yyyy-MM-dd")
				.format(c.getTime());
		return dayAfter;
	}

	/**
	 * 获取今年的第一天
	 * 
	 * @return
	 */
	public static String getFirstDay() {
		String firstDay = "";
		Calendar cal = Calendar.getInstance();
		int year = cal.get(Calendar.YEAR);
		firstDay = year + "-01-01";
		return firstDay;
	}

	/**
	 * 获取前一天的时间
	 * 
	 * @param date
	 * @return
	 */
	public static String getNextDay(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.DAY_OF_MONTH, -1);
		date = calendar.getTime();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String defaultStartDate = sdf.format(date);

		return defaultStartDate;
	}

	/**
	 * 得到指定月后的日期]
	 * 
	 * @param month
	 *            传入指定参数，比如后两个月参数为2,四个月参数为4
	 * 
	 */

	public static String getAfterMonth(int month) {
		Calendar c = Calendar.getInstance();// 获得一个日历的实例
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = null;
		try {
			date = sdf.parse(DateUtil.getCurrentTimeStr());// 初始日期
		} catch (Exception e) {

		}
		c.setTime(date);// 设置日历时间
		c.add(Calendar.MONTH, month);// 加几个月
		String strDate = sdf.format(c.getTime());// 的到你想要得几个月后的日期
		return strDate;

	}

	/**
	 * 
	 * 查询某个月前日期
	 * 
	 * 例如：一个月前，参数为-1
	 * 
	 * @param month
	 * @return
	 */
	public static String getBeforeMonth(int month) {
		Calendar c = Calendar.getInstance();// 获得一个日历的实例
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = null;
		try {
			date = sdf.parse(DateUtil.getCurrentTimeStr());// 初始日期
		} catch (Exception e) {

		}
		c.setTime(date);// 设置日历时间
		c.add(Calendar.MONTH, month);// 减几个月
		String strDate = sdf.format(c.getTime());// 的到你想要得几个月前的日期
		return strDate;
	}

	/**
	 * 
	 * 查询某个月前日期
	 * 
	 * 例如：一个月前，参数为-1
	 * 
	 * @param month
	 * @return
	 */
	public static String getBeforeMonth1(int month) {
		Calendar c = Calendar.getInstance();// 获得一个日历的实例
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		Date date = null;
		try {
			date = sdf.parse(DateUtil.getCurrentTimeStr());// 初始日期
		} catch (Exception e) {

		}
		c.setTime(date);// 设置日历时间
		c.add(Calendar.MONTH, month);// 减几个月
		String strDate = sdf.format(c.getTime());// 的到你想要得几个月前的日期
		return strDate;
	}

	/**
	 * 得到指定月后的日期]
	 * 
	 * @param month
	 *            传入指定参数，比如后两个月参数为2,四个月参数为4
	 * 
	 * @param param
	 *            日期
	 */

	public static String getAfterMonth1(int month, String param) {
		Calendar c = Calendar.getInstance();// 获得一个日历的实例
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = null;
		try {
			date = sdf.parse(param);// 初始日期
		} catch (Exception e) {

		}
		c.setTime(date);// 设置日历时间
		c.add(Calendar.MONTH, month);// 加几个月
		String strDate = sdf.format(c.getTime());// 的到你想要得几个月后的日期
		return strDate;

	}

	/**
	 * 获取指定时间之前或之后几分钟 minute
	 * 
	 * @param date
	 * @param minute
	 */

	public static String getTimeByMinute(Date date, int minute) {

		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);

		calendar.add(Calendar.MINUTE, minute);

		return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar
				.getTime());

	}

	/**
	 * TimeStamp-->String
	 * 
	 * @param timeS
	 * @return
	 */
	public static Timestamp getStringToTimeStamp1(String timeS) {
		try {
			if (StringUtil.isNullOrBlank(timeS))
				return null;
			return Timestamp.valueOf(timeS);
		} catch (Exception e) {
			logger.error("getStringToTimeStamp1 error", e);
			return null;
		}
	}

	public static int getWorkingDay(Calendar d1, Calendar d2) {
		int result = -1;
		if (d1.after(d2)) {
			Calendar swap = d1;
			d1 = d2;
			d2 = swap;
		}
		int charge_start_date = 0;
		int charge_end_date = 0;
		int stmp;
		int etmp;
		stmp = 7 - d1.get(Calendar.DAY_OF_WEEK);
		etmp = 7 - d2.get(Calendar.DAY_OF_WEEK);
		if (stmp != 0 && stmp != 6) {
			charge_start_date = stmp - 1;
		}
		if (etmp != 0 && etmp != 6) {
			charge_end_date = etmp - 1;
		}
		result = (getDaysBetween(DateUtil.getNextMonday(d1),
				DateUtil.getNextMonday(d2)) / 7)
				* 5 + charge_start_date - charge_end_date;
		return result;
	}

	public static Calendar getNextMonday(Calendar date) {
		Calendar result = null;
		result = date;
		do {
			result = (Calendar) result.clone();
			result.add(Calendar.DATE, 1);
		} while (result.get(Calendar.DAY_OF_WEEK) != 2);
		return result;
	}

	public static int getDaysBetween(Calendar d1, Calendar d2) {
		if (d1.after(d2)) {
			Calendar swap = d1;
			d1 = d2;
			d2 = swap;
		}
		int days = d2.get(Calendar.DAY_OF_YEAR) - d1.get(Calendar.DAY_OF_YEAR);
		int y2 = d2.get(Calendar.YEAR);
		if (d1.get(Calendar.YEAR) != y2) {
			d1 = (Calendar) d1.clone();
			do {
				days += d1.getActualMaximum(Calendar.DAY_OF_YEAR);
				d1.add(Calendar.YEAR, 1);
			} while (d1.get(Calendar.YEAR) != y2);
		}
		return days;
	}

	public static String getATDay(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.DAY_OF_MONTH, 15);
		date = calendar.getTime();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String defaultStartDate = sdf.format(date);

		return defaultStartDate;
	}

	public static String getCurrentYear() {
		SimpleDateFormat df = new SimpleDateFormat("yyyy");// 设置日期格式
		return df.format(new Date());
	}

	public static String getCurrentMonth() {
		SimpleDateFormat df = new SimpleDateFormat("MM");// 设置日期格式
		return df.format(new Date());
	}

	/**
	 * 两个时间相差距离多少天多少小时多少分多少秒
	 * 
	 * @param str1
	 *            时间参数 1 格式：1990-01-01 12:00:00
	 * @param str2
	 *            时间参数 2 格式：2009-01-01 12:00:00
	 * @return long[] 返回值为：{天, 时, 分, 秒}
	 */
	public static long getDistanceTimes(String str1, String str2) {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date one;
		Date two;
		long day = 0;
		long hour = 0;

		try {
			one = df.parse(str1);
			two = df.parse(str2);
			long time1 = one.getTime();
			long time2 = two.getTime();
			long diff;
			if (time1 < time2) {
				diff = time2 - time1;
			} else {
				diff = time1 - time2;
			}
			day = diff / (24 * 60 * 60 * 1000);
			hour = (diff / (60 * 60 * 1000) - day * 24);

		} catch (ParseException e) {
			e.printStackTrace();
		}
		long times = hour;
		return times;
	}

	/**
	 * 获取当前系统时间指定月份的时间
	 * 
	 * @param month
	 *            月份（+1代表当前系统后一个月，-1代表当前系统前一个月）
	 * @return 日期（例如：2017-04-22 17:13:50）
	 */
	public static String getMonthTime(int month) {
		Date date = new Date();// 当前日期
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 格式化对象
		Calendar calendar = Calendar.getInstance();// 日历对象
		calendar.setTime(date);// 设置当前日期
		calendar.add(Calendar.MONTH, month);// 月份减一

		return sdf.format(calendar.getTime());
	}

	public static String getCurrentDate() {
		SimpleDateFormat df = new SimpleDateFormat("yyyy年MM月dd日");// 设置日期格式
		return df.format(new Date());
	}

	/**
	 * 根据年 月 获取对应的月份 天数
	 */
	public static int getDaysByYearMonth(int year, int month) {

		Calendar a = Calendar.getInstance();
		a.set(Calendar.YEAR, year);
		a.set(Calendar.MONTH, month - 1);
		a.set(Calendar.DATE, 1);
		a.roll(Calendar.DATE, -1);
		int maxDate = a.get(Calendar.DATE);
		return maxDate;
	}
}
